package com.pojo.util;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

public class Tool {

    public static String getCheckCode(){
        Random r = new Random();
        char nums[]=new char[8];
        for(int i=0;i<4;i++) {
            nums[i]=(char) ('0'+r.nextInt(10));
            nums[i+4]=(char) ('a'+r.nextInt(26));
        }
        String text="";
        for(int i=0;i<4;i++) {
            text+=nums[r.nextInt(8)];
        }
        return text;
    }
    public static String MD5(String salt, String password){
        String result=password;
        for(int i=0;i<3;i++)
            result= DigestUtils.md5DigestAsHex((salt+result+salt).getBytes());
        return result;
    }
    public static  <T,R,Y>  T putDateToRedis(Function<R,T> function, R id, Y key, Y lkey, T tresult, RedisTemplate redisTemplate, RedissonClient redissonClient) throws InterruptedException {
        RLock rLock= redissonClient.getLock((String) lkey);
        boolean islock=rLock.tryLock(3,10, TimeUnit.SECONDS);
        T result=null;
        if (islock){
            try{
                result= (T) redisTemplate.opsForValue().get(key);
                if (result!=null)
                    return result;
                result = (T) function.apply(id);
                if(result==null){
                    redisTemplate.opsForValue().set(key,tresult,120,TimeUnit.MINUTES);
                }else {
                    redisTemplate.opsForValue().set(key,result,120,TimeUnit.MINUTES);
                }

            }finally {
                if (rLock.isLocked()) { // 是否还是锁定状态
                    if (rLock.isHeldByCurrentThread()) { // 时候是当前执行线程的锁
                        rLock.unlock(); // 释放锁
                    }
                }
            }
        }

        return  result;
    }
    public static   <T,R,Y>  T updateToRedis(Function<R,T> function, R id,
                                             Y key, Y lkey,
                                             RedisTemplate redisTemplate,
                                             RedissonClient redissonClient) throws InterruptedException {
        RLock rLock= redissonClient.getLock((String) lkey);
        boolean islock=rLock.tryLock(1,10, TimeUnit.SECONDS);
        T result=null;
        if (islock){
            try{
                redisTemplate.delete(key);
                result = (T) function.apply(id);
                if((Integer)result>0)
                redisTemplate.opsForValue().set(key,id,120,TimeUnit.MINUTES);
            }finally {
                if (rLock.isLocked()) { // 是否还是锁定状态
                    if (rLock.isHeldByCurrentThread()) { // 时候是当前执行线程的锁
                        rLock.unlock(); // 释放锁
                    }
                }
            }
        }

        return  result;
    }
    public static Integer getMoney(Date vBegintime, Date vEndtime,Integer cost){
        Integer hours=vEndtime.getHours()-vBegintime.getHours();
        Integer bmiu=vBegintime.getMinutes(),emiu=vEndtime.getMinutes();
        if(emiu<bmiu){
            hours--;
            emiu+=60;
        }
        Integer miu=emiu=bmiu;
        Integer vAmountOfMoney=hours*cost+miu*cost/60;
        return vAmountOfMoney;
    }

    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }
    public static String getDateToString(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        return nowTime;
    }
    public static String getDateToStringSort(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = format.format(date);
        return nowTime;
    }
    public static String getDateToStringHours(Date date){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String nowTime = format.format(date);
        return nowTime;
    }
}
